﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Flare.Pages.General
{
    public partial class Room : UserControl, ITypicalPage
    {
        public string name
        {
            get { return "Комнаты"; }
        }

        public Room()
        {
            InitializeComponent();
        }

        private void department_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RoomList.SelectedIndex == -1)
            {
                ((ObjectDataProvider)this.Resources["general_roomsDataSet"]).MethodParameters[0] = (int?)department.SelectedValue;
            }
        }

        private void RoomList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RoomList.SelectedIndex == -1)
            {
                ButtonSave.Visibility = System.Windows.Visibility.Collapsed;
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ButtonClose.Visibility = System.Windows.Visibility.Collapsed;
                ButtonAdd.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                department.SelectedValue = ((Flare.DB.Entity.General.general_room)RoomList.SelectedItem).id_department;

                ButtonSave.Visibility = System.Windows.Visibility.Visible;
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                ButtonClose.Visibility = System.Windows.Visibility.Visible;
                ButtonAdd.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            var data = new Flare.DB.CeEntity.general_room()
            {
                room = RoomName.Text,
                id_room = (int)RoomList.SelectedValue,
                id_department = (int?)department.SelectedValue
            };

            try
            {
                new DB.general_rooms().Edit(data);
                RoomList.SelectedItem = null;
                RoomName.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("general_roomsDataSet");
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (RoomList.SelectedValue == null)
            {
                glob.kernel.ShowNotice("Значение [Кабинет] должно иметь значение");
                return;
            }

            int id_room = (int)RoomList.SelectedValue;
            try
            {
                new DB.general_rooms().Delete(id_room);
                RoomList.SelectedItem = null;
                RoomName.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("general_roomsDataSet");
        }

        private void ButtonClose_Click(object sender, RoutedEventArgs e)
        {
            department.SelectedIndex = -1;
            RoomList.SelectedIndex = -1;
            ((ObjectDataProvider)this.Resources["general_roomsDataSet"]).MethodParameters[0] = null;
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            var data = new Flare.DB.CeEntity.general_room()
            {
                room = RoomName.Text,
                id_department = (int?)department.SelectedValue
            };

            try
            {
                new DB.general_rooms().Create(data);
                RoomName.Text = string.Empty;
            }
            catch (Exception ex)
            {
                glob.kernel.ShowNotice(ex.Message);
            }

            glob.kernel.RefreshResourceByName("general_roomsDataSet");
        }
    }
}
